﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScormModel.HtmlBuilder;
using System.IO;
using ScormModel;

namespace WpfELearningAuthoringTool
{
    public class HtmlExportUtil
    {
        string _identifier = "item";
        List<Item> listResourceItem;
        ImsManifest imsmanifest;
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public List<Item> ListResourceItem
        {
            get { return this.listResourceItem; }
            set { this.listResourceItem = value; }
        }
        public HtmlDocument CreateHtmlNode()
        {
            ScormModel.HtmlBuilder.HtmlElement inputForm = new ScormModel.HtmlBuilder.HtmlElement("form");
            inputForm.Nodes.Clear();
            HtmlAttribute nameAttribute = new HtmlAttribute("name", this._identifier);
            inputForm.Attributes.Add(nameAttribute);
            //add html content here
            this.buildHtmlIndexFile(inputForm);
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\\QuestionTemplates\\" + "QuestionTemplatePage.htm", System.Text.UnicodeEncoding.UTF8);

            string readHtmlCode = sr.ReadToEnd();
            sr.Close();
            ScormModel.HtmlBuilder.HtmlDocument mDocument = ScormModel.HtmlBuilder.HtmlDocument.Create(readHtmlCode, true);//sr.ReadToEnd(),true);

            ScormModel.HtmlBuilder.HtmlElement htmlElement = (ScormModel.HtmlBuilder.HtmlElement)mDocument.Nodes["HTML"];
            ScormModel.HtmlBuilder.HtmlElement questionElement = (ScormModel.HtmlBuilder.HtmlElement)htmlElement.Nodes["BODY"];
            questionElement.Nodes.Add(inputForm);
            return mDocument;
        }
        private void buildHtmlIndexFile(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            /*
             <A href="./chapter2.html" title="Get chapter two.">chapter two</A>. 
             */
            //find all resource iem in to list
            //for each
            //create action element
            ManifestProcessing mp = new ManifestProcessing(imsmanifest);
            ScormModel.HtmlBuilder.HtmlElement listElement = new ScormModel.HtmlBuilder.HtmlElement("ul");
            for (int i = 0; i < listResourceItem.Count; i++)
            {
                Item item = listResourceItem[i];
                Resource re = mp.findResourceForItem(item);
                if (re != null)
                {
                    ScormModel.HtmlBuilder.HtmlElement elementi = new ScormModel.HtmlBuilder.HtmlElement("li");
                    ScormModel.HtmlBuilder.HtmlElement infoElement = new ScormModel.HtmlBuilder.HtmlElement("A");
                    HtmlAttribute typeAttribute = new HtmlAttribute("href", re.ListFileRef[0].Bref);
                    HtmlAttribute nameAttribute = new HtmlAttribute("title",item.Title);
                    infoElement.Attributes.Add(typeAttribute);
                    infoElement.Attributes.Add(nameAttribute);

                    ScormModel.HtmlBuilder.HtmlElement textElement = new ScormModel.HtmlBuilder.HtmlElement("p");
                    textElement.Nodes.Add((HtmlNode)new HtmlText(item.Title));

                    infoElement.Nodes.Add((HtmlNode)textElement);
                    elementi.Nodes.Add((HtmlNode)infoElement);

                    listElement.Nodes.Add((HtmlNode)elementi);
                }
            }
            inputForm.Nodes.Add((HtmlNode)listElement);
        }

        private void buildCourseHeader(ScormModel.HtmlBuilder.HtmlElement inputForm)
        { 
            
        }
    }
}
